//
//  MLPictureView.swift
//  MLSina20160624
//
//  Created by manmanlang on 16/6/30.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit

let itemMargin: CGFloat = 5
let cellWH = (UIScreen_Bounds_Width -  StautsMargin * 2 - itemMargin * 2)/3
class MLPictureView: UICollectionView {

    var picture: [MLPicture]? {
        didSet {
//            countLabel.text = "\(picture?.count ?? 0)"
            let size = getPictureSize(picture?.count ?? 0)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            reloadData()
        }
    }
    
    func getPictureSize(count: Int) -> CGSize{
        
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row =  count == 4 ? 2 :(count - 1)/3 + 1
        
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: w, height: h)
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        backgroundColor = UIColor.clearColor()
        dataSource = self
        registerClass(CZPictureViewCell.self, forCellWithReuseIdentifier: "cell")
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
//        addSubview(countLabel)
//        countLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(self)
//        }
        
    }
    
//    private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 35)
    
}

extension MLPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picture?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CZPictureViewCell
        cell.picture = picture![indexPath.item]
        return cell
    }
}

class CZPictureViewCell: UICollectionViewCell {
    
    var picture: MLPicture?{
        didSet{
            bgImageView.sd_setImageWithURL(NSURL(string: picture?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        contentView.addSubview(bgImageView)
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imgName: "timeline_card_top_background")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()


}
